<?php

/**
 * @author Boomer
 * @copyright 2015
 */
session_start();
class Users extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('mod_master_user');
    }
    
  public function index()
	{
        $this->load->view('master_user/signin');
    }

    function login() {
        if ($this->input->post('barcode'))
            {
                
                $barcode = $this->input->post('barcode');
            $login_data = $this->mod_master_user->cek_user_login_barcode($barcode);
            if ($login_data == TRUE) {
                $session_data = array( 'username' => $login_data['NAMA'] , 'type' => $login_data['ROLE'] , 'id' =>$login_data['ID_USER'], 'is_login' => TRUE);
				$this->session->set_userdata($session_data);
                if ($this->session->userdata('type') == 'admin') {
                    redirect('master_user');
                } else{
                    redirect('absensi');
                }     
                
	        } else {
	           $this->session->set_flashdata('message', 'Username atau Password salah. Sayangnya, kita ga bisa kasih tau mana yang salah.');
	          redirect('users/login');
	        } $this->load->view('master_user/signin');
        }        
        
         
         
         
         
        
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'username', 'required|xss_clean');
        $this->form_validation->set_rules('password', 'password', 'required|xss_clean');

        $this->form_validation->set_error_delimiters('', '<br/>');
        if ($this->form_validation->run() == TRUE ) {
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $login_data = $this->mod_master_user->cek_user_login($username, $password);
            if ($login_data == TRUE) {
                $session_data = array( 'username' => $login_data['NAMA'] , 'type' => $login_data['ROLE'] , 'id' =>$login_data['ID_USER'], 'is_login' => TRUE);
				$this->session->set_userdata($session_data);
                if ($this->session->userdata('type') == 'admin') {
                    redirect('master_user');
                } else{
                    redirect('absensi');
                }     
                
	        } else {
	           $this->session->set_flashdata('message', 'Username atau Password salah. Sayangnya, kita ga bisa kasih tau mana yang salah.');
	          redirect('users/login');
	        }
        }        $this->load->view('master_user/signin');
    }
    
    


    function logout() {

        $data = array
            (
            'ID_USER' => 0,
            'NAMA' => 0,
            'ROLE' => 0,
            'is_login' => FALSE
        );

        $this->session->sess_destroy();
        $this->session->unset_userdata($data);

        redirect('users/login');
    }

   
}
?>